class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        """
        if not s:
            return 0
        max_length = 0
        char_dict = {}
        start, end = 0, 0
        for c in s:
            if c in char_dict and char_dict[c] >= start:
                start = char_dict[c] + 1
            char_dict[c] = end
            end += 1
            max_length = max(max_length, end - start)
        """
        max_length = 0
        char_dict = {}  # 哈希存储上次出现位置
        start = -1
        for end in range(len(s)):
            last = char_dict.get(s[end], -1)
            start = max(start, last)    # 如果当前串内有重复字符串，就把开头置为上次出现之后
            char_dict[s[end]] = end     # 更新字符出现位置
            max_length = max(max_length, end - start)
        return max_length

if __name__ == '__main__':
    c = Solution()
    s = "abcabcbb"
    print(c.lengthOfLongestSubstring(s))